package org.zhou.demo3;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Factory {
    public static Api createApi() {
        Properties properties = new Properties();

        InputStream inputStream = Factory.class.getResourceAsStream("/factory.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        Api api = null;
        try {
            api = (Api) Class.forName(properties.getProperty("ImplClass")).newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return api;
    }
}
